extends Node

const PORT = 19091
const PLAYER_SCN = preload("res://Scenes/Player.tscn")

var enet_peer = ENetMultiplayerPeer.new()

@onready var main_menu = $CanvasLayer/MainMenu
@onready var address_entry = $CanvasLayer/MainMenu/MarginContainer/VBoxContainer/AdressEntry
@onready var hud = $CanvasLayer/HUD
@onready var health_bar = $CanvasLayer/HUD/HealthBar


func _unhandled_input(_event):
	if Input.is_action_just_pressed("quit"):
		get_tree().quit()


func _on_host_button_pressed():
	main_menu.hide()
	hud.show()

	enet_peer.create_server(PORT)
	multiplayer.multiplayer_peer = enet_peer

	# сигнал вызывается каждый раз, когда кто-то подключается к серверу
	multiplayer.peer_connected.connect(add_player)
	multiplayer.peer_disconnected.connect(remove_player)

	# добавления самого сервака в игру
	add_player(multiplayer.get_unique_id())


func _on_join_button_pressed():
	main_menu.hide()
	hud.show()

	enet_peer.create_client(address_entry.text if address_entry.text else "localhost", PORT)
	multiplayer.multiplayer_peer = enet_peer


func add_player(peer_id):
	var player = PLAYER_SCN.instantiate()
	player.name = str(peer_id)
	# почему-то работает только у сервера, а клиенты появляются на земле
	player.position.y = 10
	add_child(player)

	if player.is_multiplayer_authority():
		player.health_changed.connect(update_health_bar)


func remove_player(peer_id):
	var player = get_node_or_null(str(peer_id))
	if player:
		player.queue_free()


func update_health_bar(health_value):
	health_bar.value = health_value


func _on_multiplayer_spawner_spawned(player):
	if player.is_multiplayer_authority():
		player.health_changed.connect(update_health_bar)
